package com.insurance.web;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;

import com.insurance.bean.Agent;
import com.insurance.bean.Question;
import com.insurance.bean.User;
import com.insurance.bean.Reply;
import com.insurance.service.AgentService;
import com.insurance.service.QuestionService;
import com.insurance.service.ReplyService;


public class QuestionAction {
	@Resource QuestionService questionService;
	@Resource ReplyService replyService;
	@Resource AgentService agentService;
	private String message;
	private List<Question> questions;
	private List<Reply> replys;
	private List<Agent> agents;
	private Question question;
	private Integer id;
	
	
	public List<Reply> getReplys() {
		return replys;
	}
	public void setReplys(List<Reply> replys) {
		this.replys = replys;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Question getQuestion() {
		return question;
	}
	public void setQuestion(Question question) {
		this.question = question;
	}
	
	/**
	 * list questions
	 */
	public String list(){
		this.questions = questionService.getQuestions();
		return "list";
	}
	/**
	 * question main
	 */
	public String index(){
		this.questions = questionService.getQuestions();
		this.agents = agentService.getAgentsCheckedTop5();
		return "index";
	}
	/**
	 * add question UI
	 */
	public String addUI(){
		return "add";
	}
	
	/**
	 * add a question
	 */
	public String add(){
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user==null)
			question.setUserName("浙江保险网网友");
		else 
			question.setUserName(user.getUserName());
		
		question.setQuestionTime(new Date()); 
		this.questionService.save(this.question);
		return "success";
	}
	
	/**
	 * modify question UI
	 */
	public String modifyUI(){
		this.question = this.questionService.getQuestion(this.id);
		return "modify";
	}
	
	/**
	 * modify a question
	 */
	public String modify(){
		this.questionService.update(this.question);
		this.message="modify success";
		return "message";
	}
	
	public String show(){
		this.question = this.questionService.getQuestion(this.id);
		this.replys = this.replyService.getReplysByQuestionId(this.id);
		return "success";
	}
	
	/**
	 * delete a question
	 */
	public String del(){
		this.questionService.delete(this.id);
		this.message = "delete success!";
		return "message";		
	}
	
	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
